<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   Nine
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 */
require_once 'Nine/Model.php';
class Models_Ads extends Nine_Model
{ 
    /**
     * The primary key column or columns.
     * A compound key should be declared as an array.
     * You may declare a single-column primary key
     * as a string.
     *
     * @var mixed
     */
    protected $_primary = 'ads_id';
    
    /**
     * Constructor.
     *
     * Supported params for $config are:
     * - db              = user-supplied instance of database connector,
     *                     or key name of registry instance.
     * - name            = table name.
     * - primary         = string or array of primary key(s).
     * - rowClass        = row class name.
     * - rowsetClass     = rowset class name.
     * - referenceMap    = array structure to declare relationship
     *                     to parent tables.
     * - dependentTables = array of child tables.
     * - metadataCache   = cache for information from adapter describeTable().
     *
     * @param  mixed $config Array of user-specified config options, or just the Db Adapter.
     * @return void
     */
    public function __construct($config = array())
    {
        $this->_name = $this->_prefix . 'ads';
        return parent::__construct($config); 
    } 
	
	
    public function getAllAds($condition = array(), $order = null, $count = null, $offset = null)
    {
    	 $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('a' => $this->_name))
                ->join(array('c' => $this->_prefix . 'ads_category'), 'a.ads_category_id = c.ads_category_id',array('cname' => 'name'))
                ->order($order)
                ->limit($count, $offset);
        /**
         * Conditions
         */
        if (null != @$condition['keyword']) {
            $select->where($this->getAdapter()->quoteInto('a.name LIKE ?', "%{$condition['keyword']}%"));
        }
        if (null != @$condition['ads_category_id']) {
            $select->where("a.ads_category_id = ?", $condition['ads_category_id']);
        }
        
        return $this->fetchAll($select)->toArray();
    }
    
	
 	public function increaseSorting($startPos = 1, $num = 1)
    {
        $sql = "UPDATE {$this->_name} SET sorting = sorting + " . intval($num) . " WHERE sorting >= " . intval($startPos);
        
        $this->_db->query($sql);
    }
    
    /**
     * Get all ads by category
     * @param int $catId
     * @return Zend_Db_Table_Rowset
     */
    
    public function getAllAdsByCategory($catId, $condition = array(), $order = null, $count = null, $offset = null)
    {
    	
    	
    	$select = $this->select()
    			->where('ads_category_id=?',$catId)
    			->order($order)
                ->limit($count, $offset);
    	
    	return $this->fetchAll($select);
    }
    
    
    public function getAdsbyId($adsId)
    {
    	$select = $this->select()
    			->from(array('a' => $this->_name))
    			->where('a.ads_id=?',$adsId);
    			
    	return current($this->fetchAll($select)->toArray());	
    }
    
}